<template>
  <el-card class="space-card" shadow="hover" :style="{ backgroundImage: `url(${spaceBackground})` }">
    <!-- 半透明遮罩层 -->
    <div class="card-overlay"></div>

    <!-- 卡片内容容器 -->
    <div class="card-content">
      <!-- 左侧内容：Logo、空间名称和标签 -->
      <div class="left-content">
        <!-- 空间 Logo -->
        <div class="logo-wrapper">
          <img v-if="spaceLogo" :src="spaceLogo" alt="空间 Logo" class="space-logo" />
        </div>

        <!-- 空间基本信息 -->
        <div class="space-info">
          <h3 class="space-name">{{ spaceName }}</h3>
          <div class="meta-info">
            <span class="space-type">{{ formattedSpaceType }}</span>
            <span class="create-time">{{ formattedCreateTime }}</span>
          </div>

          <!-- 价格信息 -->
          <div class="price-info">
            <el-tag v-if="isFree" class="price-tag free">免费加入</el-tag>
            <el-tag v-else class="price-tag paid">会费: {{ fee }}元/月</el-tag>
          </div>
        </div>
      </div>

      <!-- 右侧按钮组 -->
      <div class="button-group">
        <el-tooltip content="查看详情">
          <el-button @click="viewSpaceDetails" class="icon-button">
            <el-icon><MessageBox /></el-icon>
          </el-button>
        </el-tooltip>
        <el-tooltip content="修改标签">
          <el-button @click="editSpaceTags" class="icon-button">
            <el-icon><EditPen /></el-icon>
          </el-button>
        </el-tooltip>
      </div>
    </div>

    <!-- 标签区域 -->
    <div class="tags-container">
      <el-tag v-for="(tag, index) in formattedTags" :key="index" class="space-tag">
        {{ tag }}
      </el-tag>
    </div>

    <!-- 进入空间按钮 -->
    <div class="enter-space-container">
      <el-button type="primary" class="enter-space-button" @click="enterSpace">
        进入空间
      </el-button>
    </div>
  </el-card>
</template>

<script>
import { Delete, MessageBox, EditPen } from '@element-plus/icons-vue';
import { useRouter } from 'vue-router';
import dayjs from 'dayjs';

export default {
  name: 'SpaceCard',
  components: { MessageBox, EditPen, Delete },
  props: {
    spaceLogo: String,
    spaceBackground: String,
    spaceName: String,
    spaceTags: String,
    spaceType: String,
    createTime: String,
    isFree: Boolean,
    fee: Number
  },
  setup() {
    const router = useRouter();

    const enterSpace = () => {
      router.push('/space/demo'); // 跳转到 space/demo
    };

    return { enterSpace };
  },
  computed: {
    formattedTags() {
      return this.spaceTags ? this.spaceTags.split(',').map(t => t.trim()) : [];
    },
    formattedSpaceType() {
      const typeMap = {
        'OFFICIAL_SPACE': '官方空间',
        'INTEREST_SPACE': '兴趣空间',
        'PORTAL_SPACE': '门户空间'
      };
      return typeMap[this.spaceType] || '未知类型';
    },
    formattedCreateTime() {
      return this.createTime ? dayjs(this.createTime).format('YYYY-MM-DD HH:mm') : '未知时间';
    }
  },
  methods: {
    viewSpaceDetails() {
      this.$emit('view-space-details', this.spaceName);
    },
    editSpaceTags() {
      this.$emit('edit-space-tags', this.spaceName);
    }
  }
};
</script>

<style scoped>
/* 主要卡片样式，直接使用背景图 */
.space-card {
  position: relative;
  width: 400px;
  height: 240px;
  border-radius: 12px;
  overflow: hidden;
  background-size: cover;
  background-position: center;
}

/* 半透明遮罩 */
.card-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
}

/* 内容布局 */
.card-content {
  position: relative;
  z-index: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 16px;
  color: #fff;
}

/* 左侧信息 */
.left-content {
  display: flex;
  gap: 16px;
}

.logo-wrapper {
  width: 60px;
  height: 60px;
  border-radius: 12px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.1);
}

.space-logo {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.space-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.space-name {
  font-size: 18px;
  font-weight: 600;
  margin: 0;
}

.meta-info {
  display: flex;
  gap: 12px;
  font-size: 12px;
  opacity: 0.8;
}

/* 价格信息 */
.price-tag {
  margin-top: 4px;
  padding: 4px 8px;
  font-size: 12px;
  border-radius: 8px;
}

.price-tag.free {
  background: rgba(46, 204, 113, 0.2);
  color: #2ecc71;
}

.price-tag.paid {
  background: rgba(241, 196, 15, 0.2);
  color: #f1c40f;
}

/* 右侧按钮组 */
.button-group {
  display: flex;
  flex-direction: row;
  gap: 6px;
  position: absolute;
  top: 16px;
  right: 16px;
  align-items: center; /* 确保对齐 */
}

/* 统一按钮样式（方形） */
.icon-button {
  width: 36px;
  height: 36px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.2);
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: background 0.3s ease, transform 0.2s ease;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.icon-button:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.05);
}

.icon-button:active {
  transform: scale(0.95);
}

/* 进入空间按钮 */
.enter-space-container {
  position: absolute;
  bottom: 16px;
  right: 16px;
}

.enter-space-button {
  background: #409eff;
  color: white;
  padding: 8px 16px;
  font-size: 14px;
  border-radius: 6px;
  transition: background 0.3s ease;
}

.enter-space-button:hover {
  background: #66b1ff;
}

/* 标签栏 */
.tags-container {
  position: absolute;
  bottom: 16px;
  left: 16px;
  right: 100px;
  display: flex;
  gap: 8px;
  overflow-x: auto;
}

.space-tag {
  background: rgba(255, 255, 255, 0.2) !important;
  color: #fff !important;
  border: none !important;
}
</style>
